<template>
  <div class="app-container">

    <!-- 这是最上层的搜索的框和4个按钮 -->
    <el-row :gutter="10" >
      <el-col :span="10" :offset="15">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          
            <el-input
              
              placeholder="多个内容,空格分隔"
              clearable
              style="width: 240px"
              v-model="queryParams.managerMethod"
              @keyup.enter.native="handleQuery"
            />

          <el-form-item>
            <el-button @click="handleAdd">新增</el-button>
            <el-button @click="handleUpdate()">编辑</el-button>
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['scyf:riskunit:import']"
            >导入</el-button>
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['scyf:riskunit:export']"
            >导出</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <!-- 这是进入页面后展示的表格 -->
    <el-table v-loading="loading" :data="managerMethodList"  stripe border height="550" style="font-size: 16px"
      :row-style="{height:'60px'}" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="管控方式" prop="managerMethod" width="250" />
      <el-table-column label="管控措施分类1" prop="managerMethodTypeOne.typeName" :show-overflow-tooltip="true" width="230" />
      <el-table-column label="管控措施分类2" prop="managerMethodTypeTwo.typeName" :show-overflow-tooltip="true" width="230" />
      <el-table-column label="管控措施分类3" prop="thirdTypeDesc" :show-overflow-tooltip="true" width="230" />
      <el-table-column label="管控措施描述" prop="managerMethodDesc" :show-overflow-tooltip="true" width="400" />
      <el-table-column label="隐患排查内容" prop="hiddenDanger" :show-overflow-tooltip="true" width="400" />
      <el-table-column label="风险单元" prop="eventDto.sUnit.riskUnitName" :show-overflow-tooltip="true" width="280" />
      <el-table-column label="风险事件名称" prop="eventDto.eventName" :show-overflow-tooltip="true" width="270" />
      <el-table-column label="编辑时间" prop="updateTime" :show-overflow-tooltip="true" width="120" />

      <el-table-column label="操作" class-name="small-padding fixed-width" width="260">
    
      </el-table-column>
    </el-table>

    <el-row :gutter="20">
      <el-col :span="6" >
        <el-button @click="handleDelete()">批量删除</el-button>
      </el-col>
      <el-col :span="6" :offset="6">
        <el-pagination  @current-change="handleCurrentChange" 
          :page-sizes="[10, 20, 30, 40]" :page-size="10" layout="-> , sizes, prev, pager, next ,total" :total="total">
        </el-pagination>
      </el-col>
    </el-row>

        <!-- 添加或修改管控措施对话框 -->
    <el-dialog :title="title" width="60%" :visible.sync="open">
      <el-row style="border-radius: 0px">
        <el-col :span="24">
          <el-form :model="form" :rules="rules" ref="form" :label-position="left">
            <!-- 第一行 -->
            <el-row>
              <el-col :span="11">
                <el-form-item label="风险单元"  :label-width="'140px'">
                  <el-input v-model="form.riskUnitName">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="1">
                <el-button circle icon="el-icon-search" ></el-button> 
              </el-col>
              <el-col :span="5">
                <el-form-item label="执行操作"  :label-width="'140px'">
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-button type="primary" @click="selectEvent">风险事件</el-button>
              </el-col>
            </el-row>
            <!-- 第二行 -->
            <el-row>
              <el-col :span="12">
                <el-form-item label="风险事件名称" :label-width="'140px'">
                  <el-input v-model="form.eventName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="管控方式" :label-width="'140px'">
                  <el-select v-model="form.managerMethod" >
                    <el-option label="隐患排查" value="隐患排查"></el-option>
                    <el-option label="未指定" value="未指定"></el-option>
                    <el-option label="自动化监控" value="自动化监控"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            
            <el-row>
              <el-col :span="11">
                <el-form-item label="管控措施分类1"  :label-width="'140px'">
                  <el-input v-model="form.oneTypeName">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="1">
                <el-button circle icon="el-icon-search" @click="selectType(0)"></el-button> 
              </el-col>
              <el-col :span="11">
                <el-form-item label="管控措施分类2"  :label-width="'140px'">
                  <el-input v-model="form.twoTypeName">
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="1">
                <el-button circle icon="el-icon-search" @click="selectType(1)"></el-button> 
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="管控措施分类3"  :label-width="'140px'">
                  <el-input v-model="form.thirdTypeDesc">
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="管控措施描述">
                  <el-col :span="20" offset="1">
                    <el-input type="textarea" v-model="form.managerMethodDesc"></el-input>
                  </el-col>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="24">
                <el-form-item label="隐患排查内容">
                  <el-col :span="20" offset="1">
                    <el-input type="textarea" v-model="form.hiddenDanger"></el-input>
                  </el-col>
                </el-form-item>
              </el-col>
            </el-row>

          </el-form>
          
        </el-col>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择管控措施1对话框 -->
    <el-dialog :title="title" width="60%" :visible.sync="openSMMT1" >
      <el-table v-loading="loading" @selection-change="handleSelectionOneType" :data="OneType"  stripe border highlight-current-row>
        <el-table-column type="selection" width="55" align="center" >
        </el-table-column>
        <el-table-column label="标签" prop="typeName" width="300" />
        <el-table-column label="键值" prop="id" :show-overflow-tooltip="true" width="300" />
        <el-table-column label="类型" value="管理措施分类1" :show-overflow-tooltip="true" width="300" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="OneTypeSubmit">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择管控措施2对话框 -->
    <el-dialog :title="title" width="60%" :visible.sync="openSMMT2" >
      <el-table v-loading="loading" @selection-change="handleSelectionTwoType" :data="TwoType"  stripe border >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="标签" prop="typeName" width="300" />
        <el-table-column label="键值" prop="id" :show-overflow-tooltip="true" width="300" />
        <el-table-column label="类型" value="管理措施分类2" :show-overflow-tooltip="true" width="300" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="TwoTypeSubmit">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择风险事件对话框 -->
    <el-dialog :title="title" width="40%" :visible.sync="openEvent" >
      <el-table v-loading="loading" @selection-change="handleSelectionEvent" :data="Sevent"  stripe border >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="风险事件名称" prop="eventName" width="300" />
        <el-table-column label="风险单元" prop="sUnit.riskUnitName" :show-overflow-tooltip="true" width="300" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="EventSubmit">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="title" :visible.sync="openExport" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    

    
  </div>
</template>

<script>
import { listManagerMethod,deletes,getTypeOne,getTypeTwo,OneManagerMethod,getAllEvent,addManagerMethodDto,updateManagerMethodDto} from "@/api/system/managerMethod";
import { getToken } from "@/utils/auth";


export default {
  name: "ManagerMethod",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选择管控措施1时的选中数组
      oneTypeIds:[],
      // 选择管控措施2时的选中数组
      twoTypeIds:[],
      // 选择风险事件时的选中数组
      eventIds:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //修改时的数据
      eventUnitDtoForm:{undefined}, 
      //存放选中的风险事件的dto类
      Sevent: [],
      //存放选中的风险单元
      Sunit: [],
      //存放选中的一级管控措施
      OneType: [],
      //存放选中的二级管控措施
      TwoType: [],

      // 角色表格数据
      managerMethodList: [],
      options: [{
          value: '选项1',
          label: '未指定'
        }, {
          value: '选项2',
          label: '自动化监控'
        }, {
          value: '选项3',
          label: '隐患排查'
        }],
        select:"隐患排查",

      // 弹出层标题
      title: "管控措施分类1",
      // 是否显示新增管控措施的弹出层
      open: false,
      //是否显示风险事件的弹出层
      openEvent: false,
      //是否显示风险单元的弹出层
      openUnit: false,
      //是否显示管控措施1的弹出层
      openSMMT1: false,
      //是否显示管控措施2的弹出层
      openSMMT2: false,
      //是否显示管控措施的导出层
      openExport: false,

      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: "1",
          label: "全部数据权限"
        },
        {
          value: "2",
          label: "自定数据权限"
        },
        {
          value: "3",
          label: "本部门数据权限"
        },
        {
          value: "4",
          label: "本部门及以下数据权限"
        },
        {
          value: "5",
          label: "仅本人数据权限"
        }
      ],
     //导入车间信息
     upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/scyf/managerMethod/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        managerMethod: ''
      },
      // 表单参数
      form: {
        
        eventName:'',
        
        oneTypeName:'',
        twoTypeName:'',
        

        id:undefined,
        firstTypeId:undefined,
        secondTypeId:undefined,
        eventId:undefined,
        eventUnit:undefined,
        riskUnitName:'',
        managerMethod:'',
        thirdTypeDesc:'',
        hiddenDanger:'',
        managerMethodDesc:'',

      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        petName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        roleKey: [
          { required: true, message: "权限字符不能为空", trigger: "blur" }
        ],
        roleSort: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList(this.queryParams);
  },
  methods: {
    /** 查询风险控制列表 */
    getList(queryParams) {
      this.loading = true;
      listManagerMethod(this.queryParams).then(response => {
          this.managerMethodList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    //分页
    handleCurrentChange(val) {
      
            // 改变默认的页数
            this.queryParams.pageNum=val
            console.info(val)
            console.info(this.queryParams.pageNum)
            // 切换页码时，要获取每页显示的条数
            this.getList(this.queryParams)
    },
    // 主展示表的多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 管控措施1的多选框选中数据
    handleSelectionOneType(selection) {
      this.oneTypeIds = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 管控措施2的多选框选中数据
    handleSelectionTwoType(selection) {
      this.twoTypeIds = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 风险事件的多选框选中数据
    handleSelectionEvent(selection) {
      this.eventIds = selection.map(item => item.eventId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete() {
      const mmIds = this.ids;
      this.$modal.confirm('是否确认删除风险控制编号为"' + mmIds + '"的数据项？').then(function() {
        return deletes(mmIds);
      }).then(() => {
        this.getList(this.queryParams);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
      /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测试";
    },
    /**查询管控措施 */
    selectType(val){
      console.info(val);
      if(val==0){
         getTypeOne().then(response => {
          this.OneType = response.data;
          this.openSMMT1 = true;
        })
      }
      if(val==1){
        getTypeTwo().then(response => {
          this.TwoType = response.data;
          this.openSMMT2 = true;
        })
      }
      
    },
    //执行修改
    handleUpdate(){
      
      this.queryParams.eventUnitId = this.ids[0];
      if(this.queryParams.eventUnitId==null){
        alert("未选择数据");
      }else{
        this.reset();
        //将id的数据存入到form表单中
        for (var i = 0; i < this.managerMethodList.length; i++) {
            if(this.managerMethodList[i].id==this.queryParams.eventUnitId){
              this.form.id = this.queryParams.eventUnitId;
            }
        }
        //将除id的数据回显到form表单中
        OneManagerMethod(parseInt(this.queryParams.eventUnitId)).then(response => {
          // console.info(response)
          // alert(JSON.stringify(response.data))
          this.open=true;
          if(response.data.eventDto !=undefined){
            //风险单元名称
            this.form.riskUnitName=response.data.eventDto.sUnit.riskUnitName;
          }
          if(response.data.eventDto !=undefined){
            //风险事件名称
            this.form.eventName=response.data.eventDto.eventName;
          }
          //管控方式
          this.form.managerMethod=response.data.managerMethod;
          //管控措施分类1
          if(response.data.managerMethodTypeOne!=undefined){
            this.form.oneTypeName=response.data.managerMethodTypeOne.typeName;
            this.form.firstTypeId = response.data.firstTypeId;
          }
          
          //管控措施分类2
          if(response.data.managerMethodTypeTwo!=undefined){
            this.form.twoTypeName=response.data.managerMethodTypeTwo.typeName;
            this.form.secondTypeId = response.data.secondTypeId;
          }
          
          //管控措施分类3
          this.form.thirdTypeDesc=response.data.thirdTypeDesc;
          //隐患排查内容
          this.form.hiddenDanger=response.data.hiddenDanger;
          //管控措施
          this.form.managerMethodDesc=response.data.managerMethodDesc;
          
        })
      }
  
    },
    //选择风险事件
    selectEvent(){
      getAllEvent().then(response => {
          this.Sevent = response.data;
          this.openEvent = true;
        })
      this.openEvent=true;
    },
    //选中管控措施分类1后,点击确定
    OneTypeSubmit(){
      const length = this.oneTypeIds.length;
      if(length!=1){
        alert("请只选择一个属性")
      }else{
        // alert(this.oneTypeIds[0])
        //oneTypeName
        this.form.firstTypeId = this.oneTypeIds[0];
        this.firstTypeId = this.oneTypeIds[0];
        for (var i = 0; i < this.OneType.length; i++) {
            if(this.OneType[i].id==this.firstTypeId){
              this.form.oneTypeName = this.OneType[i].typeName;
            }
        }
        this.openSMMT1 = false;
      }
    },
    //选中管控措施分类2后,点击确定
    TwoTypeSubmit(){
      const length = this.twoTypeIds.length;
      if(length!=1){
        alert("请只选择一个属性")
      }else{
        // alert(this.oneTypeIds[0])
        //oneTypeName
        this.form.secondTypeId = this.twoTypeIds[0];
        this.secondTypeId = this.twoTypeIds[0];
        for (var i = 0; i < this.TwoType.length; i++) {
            if(this.TwoType[i].id==this.secondTypeId){
              this.form.twoTypeName = this.TwoType[i].typeName;
            }
        }
        this.openSMMT2 = false;
      }
    },
    //选中风险事件后,点击确定
    EventSubmit(){
      const length = this.eventIds.length;
      if(length!=1){
        alert("请只选择一个属性")
      }else{
        // alert(this.oneTypeIds[0])
        //oneTypeName
        this.form.eventId = this.eventIds[0];
        this.eventId = this.eventIds[0];
        // alert(this.eventId)
        // alert(this.eventIds.length)
        for (var i = 0; i < this.Sevent.length; i++) {
           
          // alert(this.Sevent[i].eventId)
            if(this.Sevent[i].eventId==this.eventId){
              // alert(JSON.stringify(this.Sevent[i]))
              this.form.eventName = this.Sevent[i].eventName;
              this.form.eventUnit = this.Sevent[i].sUnit.unitId;
              this.form.riskUnitName = this.Sevent[i].sUnit.riskUnitName;
            }
        }
        this.openEvent = false;
      }
    },
    //提交表单
    submitForm: function(){
      
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {

            updateManagerMethodDto(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagerMethodDto(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
        this.download('scyf/managerMethod/export', {
          ...this.queryParams
        }, `shop_manage${new Date().getTime()}.xlsx`)
      },
    /** 导入按钮操作*/
    handleImport() {
      this.title = "导入";
      this.openExport = true;
    },

    //下载模板的按钮
    importTemplate() {
      this.download('scyf/managerMethod/importTemplate', {
      }, `danger_template_${new Date().getTime()}.xlsx`)
    },
    //上传的确定按钮
    submitFileForm() {
      this.$refs.upload.submit();
      this.upload.open = false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        riskUnitName:'',
        eventName:'',
        managerMethod:'',
        oneTypeName:'',
        twoTypeName:'',
        thirdTypeDesc:'',
        hiddenDanger:'',
        managerMethod:'',

      }
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery: function() {
      alert(JSON.stringify(this.queryParams))
      // const params = this.queryParams;
      // alert(JSON.stringify(params))
      this.getList(params);
    },
  }
};
</script>

<style >
   .abow_dialog {
    display: flex;
    justify-content: center;
    align-items: Center;
    overflow: hidden;
    height: 600px;
    top: 10%;
    left: 10%;
    /* .el-dialog {
        margin: 0 auto !important;
        height: 500px;
        overflow: hidden;
        .el-dialog__body {
            position: absolute;
            left: 0;
            top: 54px;
            bottom: 0;
            right: 0;
            padding: 0;
            z-index: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    } */
}
 
</style>
